package com.xingyue.loginqueryarticle.Dao;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingyue.loginqueryarticle.entity.User;
import com.xingyue.loginqueryarticle.utils.JDBCUtils;
import jdk.nashorn.api.scripting.JSObject;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 每个方法只做一件事-复用方法
 * Created by hdwx on 2016/11/18.
 */
public class QueryDao   {
//查询数据库方法
    public User queryAll(User user){
PreparedStatement stm=null;
        ResultSet rest=null;
        JSONArray ary = new JSONArray();
        Connection con= null;

        String sql=" select * from yued";
        try {


            con = JDBCUtils.getConnection();
        stm=con.prepareStatement(sql);

             rest=stm.executeQuery();

          while  (rest.next()){
              JSONObject object= new JSONObject();

              object.put("name",rest.getString("name"));
              object.put("password",rest.getString("password"));
              object.put("id",rest.getString("id"));
                    ary.add(object);
            }
            User user1 =new User();
            user.setName(rest.getString("name"));
            user.setPassword(rest.getString("password"));
            user.setId(rest.getString("id"));

            rest.close();
            stm.close();
            con.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }




return user;

}
/*
*  更新用户最后登录时间
*
*/

public boolean updateLastLoginTime(User user){
//....数据库查询
    return true;

}

//分页查询
public PageDao queryPage(int currentPage){

        PageDao page = new PageDao();

        String sqlcount = "select count(*) as count from yued;";
        // 解析sql语句
        PreparedStatement stm = null;
        //查询
        ResultSet set=null;
        try {
            stm = JDBCUtils.getPreparedStatement(sqlcount, false);
            set=stm.executeQuery();
            set.next();  //查询数据库下一行数据

            //  set.getInt()  set.getSTring()
            int count= set.getInt(1);

            //计算总页数
            page.countPage(count);
//设置总的条数
            page.setTotalNum(count);
            if(page.getTotalPage()>=currentPage){
                String sqlLimit = "select * from yued limit ?,?";
                stm =JDBCUtils.getPreparedStatement(sqlLimit,false);
                //计算出开始获取数据的位置,(页数-1)*每页显示数据数量
                int limitstart=(currentPage-1)*page.getPageSize();
                stm.setInt(1,limitstart);  //第一个?号 显示 从limit 4n,4   -- n为当前页数>=1
//设置每页显示数量
                stm.setInt(2,page.getPageSize());
                //执行查询
                set =stm.executeQuery();
                while(set.next()){
                    JSONObject object=new JSONObject();
                    object.put("id",set.getString("id"));
                    object.put("name",set.getString("name"));
                    object.put("password",set.getString("password"));
                    page.getData().add(object);
                }
            }
            set.close();
            stm.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        page.setCurrentPage(currentPage);
        return page;   //返回page-赋值后的Page类
    }

    public static  void main(String[]ags){
        PageDao bean =  new QueryDao().queryPage(2);
        System.out.println(bean.getData().toString());
    }


    public boolean loginQuery(String name,String password){
        PreparedStatement st = null;
        ResultSet result = null;
        try{
            String sql = "select * from  yued where name=? and password=?";
            st = JDBCUtils.getPreparedStatement(sql, true);
            st.setString(1, name);
            st.setString(2, password);

            result = st.executeQuery();

                 result.close();
            st.close();
            return result.next();//返回是否有数据
        }catch(SQLException e){
            e.printStackTrace();
        }


        return false;
    }

}


